﻿<?php
ini_set('max_execution_TIME', 600);
include("dbconfig.php");


$examp = $_REQUEST["q"]; 

$page = $_REQUEST['page']; 
$limit = $_REQUEST['rows']; 
$sidx = $_REQUEST['sidx']; 
$sord = $_REQUEST['sord']; 
if(!$sidx) $sidx =1;
$style=-1;
//搜索与初始化时候传出的参数

if(isset($_GET["style"]))
	{
	$style = $_GET['style'];
	}
else{
	$style= 0;
	}
	//echo $style;
if(isset($_GET["time_mask"]))
	{
	$time_mask = $_GET['time_mask'];
	}
else{
	$time_mask= '';
	}

if(isset($_GET["rev_mask"]))
	{
	$rev_mask = $_GET['rev_mask'];
	}
else{
	$rev_mask= '';
	}
if(isset($_GET["ip_mask"]))
	{
	$ip_mask = $_GET['ip_mask'];
	}
else{
	$ip_mask= '';
	}
if(isset($_GET["num_mask"]))
	{
	$num_mask = $_GET['num_mask'];
	}
else{
	$num_mask= '';
	}
if(isset($_GET["risk_mask"]))
	{
	$risk_mask = $_GET['risk_mask'];
	}
else{
	$risk_mask= '';
	}
if($_GET["deal_mask"] != 'null'){
if(isset($_GET["deal_mask"]))
    {
	$deal_mask = $_GET['deal_mask'];
	}
else{
	$deal_mask = '';
	}

if(isset($_GET["deal_mask"][0]))
    {
	$deal_mask0 = $_GET['deal_mask'][0];
	}
else{
	$deal_mask0= '';
	}
if(isset($_GET["deal_mask"][2]))
    {
	$deal_mask1 = $_GET['deal_mask'][2];
	}
else{
	$deal_mask1= '';
	}
if(isset($_GET["deal_mask"][4]))
    {
	$deal_mask2 = $_GET['deal_mask'][4];
	}
else{
	$deal_mask2= '';
	}
if(isset($_GET["deal_mask"][6]))
    {
	$deal_mask3 = $_GET['deal_mask'][6];
	}
else{
	$deal_mask3= '';
	}
}
if(isset($_GET["from_mask"]))
   {
	$from_mask = $_GET['from_mask'];
	}
else{
	$from_mask = '';
	}
if(isset($_GET["to_mask"]))
   {
	$to_mask = $_GET['to_mask'];
	}
else{
	$to_mask = '';
	}
if($_GET["type_mask"] != 'null'){
if(isset($_GET["type_mask"]))
    {
	$type_mask = $_GET['type_mask'];
	}
else{
	$type_mask = '';
	}

if(isset($_GET["type_mask"][0]))
    {
	$type_mask0 = $_GET['type_mask'][0];
	}
else{
	$type_mask0= '';
	}
	if(isset($_GET["type_mask"][2]))
    {
	$type_mask1 = $_GET['type_mask'][2];
	}
else{
	$type_mask1= '';
	} 
}
if(isset($_GET["detail_mask"]))
	{
	$detail_mask = $_GET['detail_mask'];
	}
else{
	$detail_mask= '';
	}
if($style==0)
{
$where = " WHERE 1=1";
if($from_mask!='')
	$where.= " AND time >='$from_mask%'";
if($to_mask!='')
	$where.= " AND time <='$to_mask%'";
if($num_mask!='')
	$where.= " AND num >='$num_mask'";
if($risk_mask!='')
	$where.= " AND riskvalue >='$risk_mask'";
$where.=" AND "."( "."1=1";
if($deal_mask0!='')
	$where.= " AND deal = '$deal_mask0'";
if($deal_mask1!='')
	$where.= " OR deal = '$deal_mask1'";
if($deal_mask2!='')
	$where.= " OR deal = '$deal_mask2'";
if($deal_mask3!='')
	$where.= " OR deal = '$deal_mask3'";
$where.=")";
$where.=" AND "."( "."1=1";
if($type_mask0!='')
	$where.= " AND type = '$type_mask0'";
if($type_mask1!='')
	$where.= " OR type = '$type_mask1'";
$where.=")";
if($ip_mask!='')
    $where.= " AND reip like '%$ip_mask%'";
if($rev_mask!='')
    $where.= " AND email like '%$rev_mask%'";

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");
$SQL ="select * from black_email ".$where;
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();
$count = mysql_num_rows($result);

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL ="select * from black_email ".$where."ORDER BY $sidx $sord LIMIT $start , $limit";
//echo $SQL;
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
	 $responce->rows[$i]['ID']=$row[id];
	 	switch($row[deal]){
   case 0: $row[deal]="阻断";break;
   case 1: $row[deal]="隔离";break;
   case 2: $row[deal]="重定向";break;
   case 3: $row[deal]="欺骗";break;
    }
    switch($row[type]){
   case 0: $row[type]="木马传输";break;
   case 1: $row[type]="非法地址";break;
    }
    $responce->rows[$i]['cell']=array($row[id],$row[time],$row[email],$row[type],$row[riskvalue],$row[num],$row[deal],$row[reip],$row[detail]);
    $i++;
} 

echo json_encode($responce);
mysql_close($db);

}

else if($style==1){

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");

$sql="select count(*) as count from black_email where email = '$rev_mask' ";
$result =mysql_query ($sql);
if (!$result) echo "SQL错误0：".mysql_error();
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count==0)
{
$sql="Insert into black_email(email,deal,riskvalue,type,time,reip,detail) values('$rev_mask','$deal_mask','$risk_mask','$type_mask','$time_mask','$ip_mask','$detail_mask')";
$result =mysql_query ($sql);
if (!$result) echo "SQL错误1：".mysql_error();
}
else
{
	 echo "已经存在";
}

$SQL ="select * from black_email ".$where;
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();
$count = mysql_num_rows($result);

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL ="select * from black_email ORDER BY $sidx $sord LIMIT $start , $limit";
//echo $SQL;
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
	 $responce->rows[$i]['ID']=$row[id];
	 	switch($row[deal]){
   case 0: $row[deal]="阻断";break;
   case 1: $row[deal]="隔离";break;
   case 2: $row[deal]="重定向";break;
   case 3: $row[deal]="欺骗";break;
    }
    switch($row[type]){
   case 0: $row[type]="木马传输";break;
   case 1: $row[type]="非法地址";break;
    }
    $responce->rows[$i]['cell']=array($row[id],$row[time],$row[email],$row[type],$row[riskvalue],$row[num],$row[deal],$row[reip],$row[detail]);
    $i++;
} 

echo json_encode($responce);
mysql_close($db);
}

else if($style==2)
{
   $db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
   or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
   mysql_select_db($database) or die("Error conecting to db.");
   	switch ($_REQUEST['oper']) {
		case 'del':
			delete();
			break;
		case 'edit':
			update($_POST);
			break;
		default:
			break;
	}
	mysql_close($db);
} 

else if($style==3)
{
   $db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
   or die("Connection Error: " . mysql_error());
   mysql_query("SET names UTF8");
   mysql_select_db($database) or die("Error conecting to db.");
   upload();
	mysql_close($db);

?>
<?php $url = "mail_manager_ft.php";?>
   <html>
   <head>
   <meta http-equiv="refresh" content="3; url=<?php echo $url; ?>">
   </head>
   <body>
3秒种后跳转
</body>
</html>	
<?php }?>




<?php
function delete(){
		 $id=$_REQUEST['id'];
		 $var=explode(",",$id);
		 $len=count($var);
		 for ($i=0;$i<$len;$i++)
		 {
		 $sql="Delete from black_email where id='$var[$i]'";
		 $result =mysql_query ($sql); 
		 if (!$result) echo "SQL错误：".mysql_error();
		 }			 
	    }

function update($data) {
		  $id=$_REQUEST['ID'];
		  $email=$_REQUEST['EMAIL'];
		  $type=$_REQUEST['TYPE'];
		  $deal=$_REQUEST['DEAL'];
		  $risk=$_REQUEST['RISKVAL'];
		  $time=$_REQUEST['DATE'];
		  //$reip=$_REQUEST['REIP'];
		  $detail=$_REQUEST['DETAIL'];
		 $sql="Update black_email set email='$email',type='$type',deal='$deal',riskvalue='$risk',detail='$detail',time='$time' where id='$id'";
		 $result =mysql_query ($sql);
		 if (!$result) echo "SQL错误：".mysql_error();

	 }

function upload($data){
if ((($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "text/txt"))
&& ($_FILES["file"]["size"] < 2000000))
{
	if ($_FILES["file"]["error"] > 0)
	{
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	}
	else
	{

			move_uploaded_file($_FILES["file"]["tmp_name"],
			"upload/" . $_FILES["file"]["name"]);
			$countre=0;
			$target_path = 'upload/';
			$target_path = $target_path . $_FILES["file"]["name"];
			$hander= fopen($target_path,"r");
			while(!feof($hander))
			{
				$content=fgets($hander);
				$tmpmail = strtok($content," ");
				$count++;
				$tmptype=strtok(" ");
				//$tmpctl=strtok(" ");
				//$tmpreip=strtok(" ");
				$tmpdetail=strtok(" ");
				if($tmpreip==0)
				{
					$tmpreip="";
				}

				$tmptime=date("Y-m-d");

				if($tmpmail!="")
				{
					$sql="select count(*) as count from black_email where email = '$tmpmail' ";
					$result= mysql_query($sql);
					if (!$result) echo "SQL错误：".mysql_error();
					$row = mysql_fetch_array($result,MYSQL_ASSOC);
					$count = $row['count'];
					if($count==0)
					{
						$sql="Insert into black_email(email,time,type,detail) values('$tmpmail','$tmptime','$tmptype','$tmpdetail')";
						
						$result =mysql_query ($sql);
						if (!$result) echo "SQL错误1：".mysql_error();
					}
					else
					{
						$countre++;
					}
				}

			}

			fclose($hander);
			echo "重复条目数: " .$countre;
	}
}
else
{
	echo "Invalid file";

}



}



?>
